package com.zsc.yygh.order.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.yygh.common.result.Result;
import com.zsc.yygh.common.utils.AuthContextHolder;
import com.zsc.yygh.enums.OrderStatusEnum;
import com.zsc.yygh.model.order.OrderInfo;
import com.zsc.yygh.model.order.PaymentInfo;
import com.zsc.yygh.model.order.RefundInfo;
import com.zsc.yygh.order.service.OrderService;
import com.zsc.yygh.order.service.PaymentService;
import com.zsc.yygh.order.service.RefundInfoService;
import com.zsc.yygh.vo.order.OrderCountQueryVo;
import com.zsc.yygh.vo.order.OrderQueryVo;
import com.zsc.yygh.vo.order.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/order/orderInfo")
@Api(tags = "用户订单")
public class OrderApiController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private RefundInfoService refundInfoService;


    //生成挂号订单
    @ApiOperation(value = "生成挂号订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result savaOrders(@PathVariable String scheduleId,
                             @PathVariable Long patientId) {
        Long orderId = orderService.saveOrder(scheduleId,patientId);
        return Result.ok(orderId);
    }

    //根据订单id查询订单详情
    @ApiOperation(value = "根据订单id查询订单详情")
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable String orderId) {
        OrderInfo orderInfo = orderService.getOrder(orderId);
        return Result.ok(orderInfo);
    }


    //订单列表（条件查询带分页）
    @ApiOperation(value = "订单列表（条件查询带分页）")
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       OrderQueryVo orderQueryVo, HttpServletRequest request) {
        //设置当前用户id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page,limit);
        IPage<OrderInfo> pageModel =
                orderService.selectPage(pageParam,orderQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    //取消预约
    @GetMapping("auth/cancelOrder/{orderId}")
    @ApiOperation(value = "取消预约")
    public Result cancelOrder(@PathVariable Long orderId) {
        Boolean isOrder = orderService.cancelOrder(orderId);
        return Result.ok(isOrder);
    }

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }

    @ApiOperation(value = "获取订单数量")
    @GetMapping("/getOrderCount")
    public Result countOrder(){
        int count = orderService.count();
        return Result.ok(count);
    }

    @ApiOperation(value = "统计订单状态")
    @PostMapping("inner/getOrder")
    public Map<String, Object> getOrder(@RequestBody OrderCountQueryVo orderCountQueryVo){
//        System.out.println("==========================="+orderService.getOderMap());
        return orderService.getOderMap(orderCountQueryVo);
    }

    @ApiOperation(value = "统计未支付")
    @PostMapping("inner/getOrderPl")
    public Map<String, Object> getOrderPl(@RequestBody OrderCountQueryVo orderCountQueryVo){
//        System.out.println("==========================="+orderService.getOderMap());
        return orderService.getOderPl(orderCountQueryVo);
    }

    @ApiOperation(value = "统计已支付")
    @PostMapping("inner/getOrderYPl")
    public Map<String, Object> getOrderYPl(@RequestBody OrderCountQueryVo orderCountQueryVo){
//        System.out.println("==========================="+orderService.getOderMap());
        return orderService.getOderYPl(orderCountQueryVo);
    }

    @ApiOperation(value = "查询所有订单")
    @GetMapping("/findAllOrders")
    public Result findAllOrders(){
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.orderByDesc("create_time");
        paymentInfoQueryWrapper.last("limit 4");

        List<PaymentInfo> paymentInfoList = paymentService.list(paymentInfoQueryWrapper);

        List<OrderInfo> orderInfoList = new ArrayList<>();
        for (PaymentInfo paymentInfo : paymentInfoList) {
            orderInfoList.add(orderService.getById(paymentInfo.getOrderId()));
        }
        return Result.ok(orderInfoList);
    }

}
